

(function ($) {
	$.msg = function (msg, cb) {
		layer.msg(msg, {
			title: false,
			closeBtn: false,
			success: function () {
				cb && cb()
			}
		})
	}
	$.submit = function (url, data, cb) {
		var load = layer.load(1);
		$.post(url, data, r => {
			layer.close(load)
			if (cb) return cb(r);
			layer.alert(r.msg, {
				title: false,
				closeBtn: false,
				success: function () {
					this.enterEsc = function (event) {
						if (event.keyCode === 13 || event.keyCode === 32 || event.keyCode == 27) {
							$(".layui-layer-btn0").click();
							return false; //阻止系统默认回车事件
						}
					};
					$(document).on('keydown', this.enterEsc); //监听键盘事件，关闭层
				},
				end: function (index) {
					$(document).off('keydown', this.enterEsc); //解除键盘关闭事件
					r.url && (window.location.href = r.url)
					return false;
				}
			})
		}, 'json')
	}
	$.upload = function (url, data, cb) {
		var load = layer.load(1);
		$.ajax({
			url: url,
			data: data,
			type: 'POST',
			dataType: 'JSON',
			cache: false,
			processData: false,
			contentType: false,
			success: function (r) {
				layer.close(load)
				if (cb) return cb(r);
				layer.alert(r.msg, {
					title: false,
					closeBtn: false,
					success: function () {
						this.enterEsc = function (event) {
							if (event.keyCode === 13 || event.keyCode === 32 || event.keyCode == 27) {
								$(".layui-layer-btn0").click();
								return false; //阻止系统默认回车事件
							}
						};
						$(document).on('keydown', this.enterEsc); //监听键盘事件，关闭层
					},
					end: function (index) {
						$(document).off('keydown', this.enterEsc); //解除键盘关闭事件
						r.url && (window.location.href = r.url)
						return false;
					}
				})
			}
		})
	}
	$.fn.serializeJSON = function () {
		var o = {};
		var a = this.serializeArray();
		$.each(a, function () {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			} else {
				o[this.name] = this.value || '';
			}
		});
		return o;
	};
	$.confirm = function (msg, cb) {

		layer.confirm(msg, {
			title: false,
			closeBtn: false,
			success: function () {
				this.enterEsc = function (event) {
					if (event.keyCode === 13 || event.keyCode === 32) {
						$(".layui-layer-btn0").click();
						return false; //阻止系统默认回车事件
					} else if (event.keyCode == 27) {
						$(".layui-layer-btn1").click();
						return false;
					}
				};
				$(document).on('keydown', this.enterEsc); //监听键盘事件，关闭层
			},
			yes: function (index) {
				$(document).off('keydown', this.enterEsc); //解除键盘关闭事件
				cb && cb()
			}
		});
	}
	$.prompt = function (title, value = '', cb) {
		layer.prompt({
			formType: 0,
			placeholder: value,
			title: title,
		}, function (value, index, elem) {

			cb && cb(value)
		});
	}
	$.alert = function (r, cb) {
		layer.alert(r.msg, {
			title: false,
			closeBtn: false,
			success: function () {
				this.enterEsc = function (event) {
					if (event.keyCode === 13 || event.keyCode === 32 || event.keyCode == 27) {
						$(".layui-layer-btn0").click();
						return false; //阻止系统默认回车事件
					}
				};
				$(document).on('keydown', this.enterEsc); //监听键盘事件，关闭层
			},
			end: function (index) {
				$(document).off('keydown', this.enterEsc); //解除键盘关闭事件
				if (cb) {
					return cb(index);
				}
				r.url && (window.location.href = r.url)
				return false;
			}
		})
	}
	$.parseParams = function (data) {
		var tempArr = [];
		for (var i in data) {
			var key = encodeURIComponent(i);
			var value = encodeURIComponent(data[i]);
			tempArr.push(key + '=' + value);
		}
		var urlParamsStr = tempArr.join('&');
		return urlParamsStr;
	}
	$.getParams = function (url) {
		var hash;
		var myJson = {};
		var hashes = url.slice(url.indexOf('?') + 1).split('&');
		for (var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			myJson[hash[0]] = hash[1];
		}
		return myJson;
	}
})(jQuery);

var myMixin = {
	created: function () {

	},
	methods: {
		getQueryVariable: function (variable) {
			var query = window.location.search.substring(1);
			var vars = query.split("&");
			for (var i = 0; i < vars.length; i++) {
				var pair = vars[i].split("=");
				if (pair[0] == variable) { return pair[1]; }
			}
			return (false);
		},
		submit: function () {
			$.submit('', this.form)
		},
		del(where,url){
			$.confirm('确定要删除吗?',function(){
				$.submit(url?url:'del',where)
			})
		}
	}
};
Vue.mixin(myMixin)